﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using Monitor.Core.Utilities;

namespace JunctionTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            sourceFolder.Text = Properties.Settings.Default.SourceDirectory;
            destinationFolder.Text = Properties.Settings.Default.DestinationDirectory;

            if (validateDirectories())
            {
                populateLists();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DirectoryInfo di = new DirectoryInfo(@"c:\Windows");

            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                sourceTree.Nodes.Add(dir.Name);
                sourceTree.Sort();
            }
        }

        private void createJunction_Click(object sender, EventArgs e)
        {
            if (sourceTree.SelectedNode != null)
            {
                JunctionPoint.Create(@"c:\Test2\" + sourceTree.SelectedNode.Text, sourceFolder.Text + @"\" + sourceTree.SelectedNode.Text, true);
                destinationTree.Nodes.Add(sourceTree.SelectedNode.Text);
                sourceTree.Nodes.Remove(sourceTree.SelectedNode);
            }
            destinationTree.Sort();
        }

        private void removeJunction_Click(object sender, EventArgs e)
        {
            if (destinationTree.SelectedNode != null)
            {
                if (JunctionPoint.Exists(@"c:\Test2\" + destinationTree.SelectedNode.Text))
                {
                    JunctionPoint.Delete(@"c:\Test2\" + destinationTree.SelectedNode.Text);
                }
                sourceTree.Nodes.Add(destinationTree.SelectedNode.Text);
                destinationTree.Nodes.Remove(destinationTree.SelectedNode);
            }
            sourceTree.Sort();
            sourcePicture.Image = null;
            targetPicture.Image = null;
        }

        private void selectSource_Click(object sender, EventArgs e)
        {
            selectFolder.SelectedPath = @"c:\Windows";

            if (selectFolder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                sourceTree.Nodes.Clear();

                sourceFolder.Text = selectFolder.SelectedPath;

                if (validateDirectories())
                {
                    populateLists();
                }

                Properties.Settings.Default.SourceDirectory = sourceFolder.Text;
            }
        }

        private void selectDestination_Click(object sender, EventArgs e)
        {
            if (selectFolder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                sourceTree.Nodes.Clear();

                destinationFolder.Text = selectFolder.SelectedPath;

                if (validateDirectories())
                {
                    populateLists();
                }

                Properties.Settings.Default.DestinationDirectory = destinationFolder.Text;
            }
        }

        private void sourceTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if(Directory.Exists(sourceFolder.Text + @"\" + sourceTree.SelectedNode.Text))
            {
                if (File.Exists(sourceFolder.Text + @"\" + sourceTree.SelectedNode.Text + @"\folder.jpg"))
                {
                    Image image = Image.FromFile(sourceFolder.Text + @"\" + sourceTree.SelectedNode.Text + @"\folder.jpg");

                    sourcePicture.Image = image;
                }
                else
                {
                    sourcePicture.Image = null;
                }
            }
        }

        private void destinationTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (Directory.Exists(destinationFolder.Text + @"\" + destinationTree.SelectedNode.Text))
            {
                if (File.Exists(destinationFolder.Text + @"\" + destinationTree.SelectedNode.Text + @"\folder.jpg"))
                {
                    Image image = Image.FromFile(destinationFolder.Text + @"\" + destinationTree.SelectedNode.Text + @"\folder.jpg");

                    targetPicture.Image = image;
                }
                else
                {
                    targetPicture.Image = null;
                }
            }
        }

        private bool validateDirectories()
        {
            bool result = false;

            if (!string.IsNullOrWhiteSpace(destinationFolder.Text) &&
                !string.IsNullOrWhiteSpace(sourceFolder.Text) &&
                 Directory.Exists(destinationFolder.Text) &&
                 Directory.Exists(sourceFolder.Text))
            {
                result = true;
            }

            return result;
        }

        private void populateLists()
        {
            sourceTree.Nodes.Clear();
            destinationTree.Nodes.Clear();

            DirectoryInfo di = new DirectoryInfo(sourceFolder.Text);

            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                if (Directory.Exists(destinationFolder.Text + @"\" + dir.Name))
                {
                    if (JunctionPoint.Exists(destinationFolder.Text + @"\" + dir.Name))
                    {
                        destinationTree.Nodes.Add(dir.Name);
                    }
                }
                else
                {
                    sourceTree.Nodes.Add(dir.Name);
                }
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Properties.Settings.Default.Save();
        }

    }
}
